import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shop/components/shop_search_bar.dart';
import 'package:flutter_shop/pages/home/components/home_banner.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
  /// 首页数据
  Map pageData = {};
  /// 首页banner数据
  List bannerList = [];
  @override
  void initState() {
    super.initState();
    //获取首页数据
    _getHomeData();
  }
  //获取首页数据方法
  void _getHomeData() async {
    //1.创建dio对象
    final  dio = Dio();
    //2.发送请求
    final res = await dio.get('http://smart-shop.itheima.net/index.php?s=/api/page/detail');
    //4.更新状态
    setState(() {
      pageData = res.data['data']['pageData'];
      // 赋值bannerList
      bannerList = pageData['items'][1]['data'];
    });
    // print( pageData['items'][1]['data']);
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        //背景颜色
        backgroundColor: Colors.red,
        //前景颜色：控制文字和图标颜色
        foregroundColor: Colors.white,
        // 标题居中
        centerTitle: true,
        title: const Text('智慧商城'),
      ),
      body:  CustomScrollView(
        slivers: [
          //搜索框
        const  SliverToBoxAdapter(
            child: ShopSearchBar(),
          ),
          //轮播图
          SliverToBoxAdapter(
            child: HomeBanner(bannerList: bannerList),
          ),
        ],
      ),
    );
  } 
  }